package com.jinlia.show.modules.base.system.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jinlia.show.modules.base.system.entity.MemberCardBalancePool;
import com.jinlia.show.modules.base.system.mapper.MemberCardBalancePoolMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;

@Service
@RequiredArgsConstructor
public class MemberCardBalancePoolMapperService extends ServiceImpl<MemberCardBalancePoolMapper, MemberCardBalancePool> {


    public MemberCardBalancePool updateAvailableBalance(BigDecimal changeBalance) {
        this.lambdaUpdate()
                .setSql("available_balance_sum = available_balance_sum + {0}", changeBalance)
                .update();
        return this.getById(1);
    }

    /**
     *
     * @param actualRecharge 充值金额
     * @param giftRecharge 赠送金额
     * @return
     */
    public MemberCardBalancePool updateRechargeSum(BigDecimal actualRecharge, BigDecimal giftRecharge) {
        this.lambdaUpdate()
                .setSql(!ObjectUtils.isEmpty(actualRecharge), "actual_recharge_sum = actual_recharge_sum + {0}", actualRecharge)//充值
                .setSql(!ObjectUtils.isEmpty(giftRecharge), "gift_recharge_sum = gift_recharge_sum + {0}", giftRecharge)//赠送
                .setSql("available_balance_sum = available_balance_sum + {0}", actualRecharge.add(giftRecharge)) //可用新增
                .update();
        return this.getById(1);
    }
}
